import { Alova } from '@/utils/http/alova';

export interface DictConfig {
  id: number;
  label : string;
  value:  string;
  sort: number;
  isDefault:  boolean;
  remark: string;
}

export interface EmailConfig{
  originator: string;
  smtpServer: string;
  smtpPort: string;
  smtpUsername: string;
  smtpPassword: string;
  smtpTestContent: string;
}


export interface PushConfig {
  ID: number;
  PushType: string;
  Name: string;
  PushUrl: string;
  SecretKey: string;
  AccessSecret: string;
  Enable: boolean;
  Remark: string;
}

export interface StorageConfig {
  id: number,
  name: string,
  storageType: string,
  endpoint: string,
  secretKey: string,
  accessSecret: string,
  enable: true
  remark: string
}

export function getFileStorageConfig() {
    return  Alova.Get<StorageConfig[]>('/storage-config/list');
}

export function addFileStorageConfig(params: StorageConfig) {
    return  Alova.Post('/config/dict-data/create',params);
}

export function editFileStorageConfig(id,params: StorageConfig) {
  return  Alova.Post('/config/dict-data/update'+id,params);
}

export async function getPushConfigList(){

    return Alova.Get<PushConfig[]>('/push-config/list');
}

export function addPushConfig(params: PushConfig) {
  return  Alova.Post('/config/dict-data/create',params);
}


export function editPushConfig(id,params: PushConfig) {
  return  Alova.Post('/config/dict-data/update/'+id,params);
}

export async function getEmailConfig(): Promise<EmailConfig> {
  const emailConfig: EmailConfig = {
    originator: '',
    smtpServer: '',
    smtpPort: '',
    smtpUsername: '',
    smtpPassword: '',
    smtpTestContent: '',
  };

  try {
    // 等待异步请求完成
    const response = await Alova.Get<DictConfig[]>('/config/dict-data/email_config');

    // 使用更安全的遍历方式
    response.forEach(item => {
      switch(item.label) {
        case 'originator':
          emailConfig.originator = item.value;
          break;
        case 'smtp_server':
          emailConfig.smtpServer = item.value;
          break;
        case 'smtp_port':
          emailConfig.smtpPort = item.value;
          break;
        case 'smtp_username':
          emailConfig.smtpUsername = item.value;
          break;
        case 'smtp_password':
          emailConfig.smtpPassword = item.value;
          break;
      }
    });

    return emailConfig;
  } catch (error) {
    console.error('获取邮件配置失败:', error);
    return emailConfig; // 返回空配置而不是抛出错误
  }
}
